using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Advertisement.Resources;

namespace Advertisement
{
    public class AdsComponent : DrawableGameComponent
    {

        float time_before_exit, elapsed_time, bg_rot;
        Texture2D bg, sb, en, ebr, ip;
        Vector2 bg_pos, sb_pos, en_pos, ebr_pos, ip_pos, fr_s, sn_s;
        SpriteBatch spriteBatch;
        SpriteFont font;

        public AdsComponent(Game game, float time_before_exit)
            : base(game)
        {
            this.time_before_exit = time_before_exit;
        }

        public override void Initialize()
        {
            elapsed_time = time_before_exit;
            bg_rot = 0;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            var isLandscape = Game.GraphicsDevice.Viewport.Width > Game.GraphicsDevice.Viewport.Height ? true : false;

            bg = Game.Content.Load<Texture2D>("../AdvertisementContent/ads_bg");
            sb = Game.Content.Load<Texture2D>("../AdvertisementContent/sb_ads" + (isLandscape ? "" : "_p"));
            ip = Game.Content.Load<Texture2D>("../AdvertisementContent/ideopuzzle_ads");
            en = Game.Content.Load<Texture2D>("../AdvertisementContent/enigma_ads");
            ebr = Game.Content.Load<Texture2D>("../AdvertisementContent/ebookreader_ads");

            font = Game.Content.Load<SpriteFont>("../AdvertisementContent/Regular36");

            if (isLandscape)
            {
                bg_pos = Vector2.Zero;
                en_pos = new Vector2(517,243);
                ebr_pos = new Vector2(102,243);
                sb_pos = new Vector2(517,53);
                ip_pos = new Vector2(310,243);
                fr_s = new Vector2(290 - font.MeasureString(Lang.First_S).X / 2.0f, 60);
                sn_s = new Vector2(290 - font.MeasureString(Lang.Second_S).X / 2.0f, 110);
            }
            else
            {
                bg_rot = MathHelper.ToRadians(90);

                bg_pos = new Vector2(bg.Height, 0);
                en_pos = new Vector2(48,534);
                ebr_pos = new Vector2(48,260);
                sb_pos = new Vector2(256,560);
                ip_pos = new Vector2(256,260);
                fr_s = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - font.MeasureString(Lang.First_S).X / 2.0f, 90);
                sn_s = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - font.MeasureString(Lang.Second_S).X / 2.0f, 135);
            }

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            elapsed_time -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (elapsed_time <= 0)
                Game.Exit();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            spriteBatch.Draw(bg, bg_pos, null, Color.White, bg_rot, Vector2.Zero, 1, SpriteEffects.None, 0);

            spriteBatch.Draw(sb, sb_pos, Color.White);
            spriteBatch.Draw(en, en_pos, Color.White);
            spriteBatch.Draw(ip, ip_pos, Color.White);
            spriteBatch.Draw(ebr, ebr_pos, Color.White);

            spriteBatch.DrawString(font, Lang.First_S, fr_s, Color.White);
            spriteBatch.DrawString(font, Lang.Second_S, sn_s, Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
